package com.zjdiepu.www.biz.dh;

import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zjdiepu.www.base.core.BaseException;
import com.zjdiepu.www.base.core.BaseService;
import com.zjdiepu.www.base.enums.type.OperateType;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.dao.dh.DhOverdueListHistoryMapper;
import com.zjdiepu.www.ibiz.dh.IDhOverdueListHistoryService;
import com.zjdiepu.www.model.dh.dto.DhOverdueList;
import com.zjdiepu.www.model.dh.dto.DhOverdueListHistory;
import com.zjdiepu.www.model.dh.ext.DhOverdueListHistoryExt;

/**
 * dh_逾期客户历史信息Service层操作实现<br/>
 * @author zhangzhengjian<br/>
 * @time 2017年09月04日 16时09分<br/>
 * @since 1.0<br/>
 */
@Service
public class DhOverdueListHistoryServiceImpl extends BaseService implements IDhOverdueListHistoryService {
	public static final Logger logger = LoggerFactory.getLogger(DhOverdueListHistoryServiceImpl.class);

	@Autowired
	private DhOverdueListHistoryMapper dhOverdueListHistoryMapper;
	
	@Override
	public void saveUpdateAndDelete(DhOverdueListHistory dhOverdueListHistory, OperateType operType) {
		switch (operType) {
		case add:
			dhOverdueListHistory.initInsertData(dhOverdueListHistory.getModifier(), dhOverdueListHistory.getModifierName());
			dhOverdueListHistoryMapper.insert(dhOverdueListHistory);
			break;
		case edit:
			dhOverdueListHistory.initUpdateData(dhOverdueListHistory.getModifier(), dhOverdueListHistory.getModifierName());
			// 影响的记录数
			int updateCount = dhOverdueListHistoryMapper.updateByPrimaryKeySelective(dhOverdueListHistory);
			if(updateCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(updateCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		case delete:
			dhOverdueListHistory.initDeleteData(dhOverdueListHistory.getModifier(), dhOverdueListHistory.getModifierName());
			// 影响的记录数
			int deleteCount = dhOverdueListHistoryMapper.updateByPrimaryKeySelective(dhOverdueListHistory);
			if(deleteCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(deleteCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		default:
			break;
		}
	}
	
	@Override
	public List<DhOverdueListHistory> selectByExt(DhOverdueListHistoryExt dhOverdueListHistoryExt) {
		return dhOverdueListHistoryMapper.selectByExt(dhOverdueListHistoryExt);
	}
	
	@Override
	public int countByExt(DhOverdueListHistoryExt dhOverdueListHistoryExt){
		return dhOverdueListHistoryMapper.countByExt(dhOverdueListHistoryExt);
	}
	
	@Override
	public DhOverdueListHistory selectById(String id) {
		return dhOverdueListHistoryMapper.selectByPrimaryKey(id);
	}
	
	@Override
	public void clearEmpty(Set<String> fieldNames, String id) {
		if(CollectionUtils.isNotEmpty(fieldNames)){
			dhOverdueListHistoryMapper.clearEmpty(fieldNames, id);
		}
	}
	
	@Override
	public int countRefined(DhOverdueListHistory dhOverdueListHistory) {
		return dhOverdueListHistoryMapper.countRefined(dhOverdueListHistory);
	}

	@Override
	public DhOverdueListHistory selectRefined(DhOverdueListHistory dhOverdueListHistory) {
		return dhOverdueListHistoryMapper.selectRefined(dhOverdueListHistory);
	}

	@Override
	public boolean repeat(DhOverdueListHistory dhOverdueListHistory) {
		int count = countRefined(dhOverdueListHistory);
		if(count > 1) {
			return true;
		}
		if(count == 0) {
			return false;
		}
		
		if(StringUtils.isBlank(dhOverdueListHistory.getId())) {
			return true;
		}
		DhOverdueListHistory dbItem = selectRefined(dhOverdueListHistory);
		if(null == dbItem) {
			return false;
		}
		return !dhOverdueListHistory.getId().equals(dbItem.getId());
	}
	
	@Override
	public int updateBatch(String modifier, String modifierName, Integer dpStatus, List<String> ids) {
		Integer current = (int)(System.currentTimeMillis() / 1000);
		return dhOverdueListHistoryMapper.updateBatch(modifier, modifierName, current, dpStatus, ids);
	}

	@Override
	public void insertBatch(List<DhOverdueList> listDhOverdue) {
		dhOverdueListHistoryMapper.insertBatch(listDhOverdue);
		
	}

	@Override
	public void deleteByBankCode(Map<String, Object> map) {
		dhOverdueListHistoryMapper.deleteByBankCode(map);
		
	}
}
